﻿using AutoMapper;
using MediatR;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Read.Api.Applicantion.Command.RBAC;
using ZGLXM.Read.Api.Dtos;

namespace ZGLXM.Read.Api.Applicantion.Handler.RBAC
{
    public class GetUserCommandHandler:IRequestHandler<GetUserCommand, ApiResult>
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;
        public GetUserCommandHandler(IUserRepository userRepository, IMapper mapper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            var user = await userRepository.GetAllAsync(x => x.IsDeleted == false);
            var list = mapper.Map<IList<GetUserDTO>>(user);
            return ApiResult.OK(list);
        }
    }
}
